import { fileURLToPath, URL } from "node:url";

import { defineConfig, loadEnv, splitVendorChunkPlugin } from "vite";
import type { ConfigEnv } from "vite";

import { warpperEnv } from "./build/index";
import { include, exclude } from "./build/depsOptimize";
import { loadPlugins } from "./build/plugins";

/** 当前执行node命令时文件夹的地址（工作目录） */
const root: string = process.cwd();

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }: ConfigEnv) => {
  const { VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } = warpperEnv(
    loadEnv(mode, root)
  );
  return {
    base: VITE_PUBLIC_PATH,
    plugins: [
      ...loadPlugins(command, VITE_COMPRESSION),
      splitVendorChunkPlugin(),
    ],
    // plugins: loadPlugins(command, VITE_COMPRESSION),
    server: {
      host: "0.0.0.0",
      port: VITE_PORT,
      open: true,
      cors: true,
      hmr: true, // 开启热更新
      proxy: {
        "/cupSystemApi": {
          // target: "http://10.10.87.168:9080/cup-system",
          // target: "http://192.168.11.91:30755/cup-system",
          target: "http://192.168.11.91:31310/cup-system",
          ws: true,
          rewrite: (path) => path.replace(/^\/cupSystemApi/, "/"),
        },
        "/cupUserApi": {
          // target: "http://10.10.87.168:9080/cup-user",
          // target: "http://192.168.11.91:30755/cup-user",
          target: "http://192.168.11.91:31310/cup-user",
          ws: true,
          rewrite: (path) => path.replace(/^\/cupUserApi/, "/"),
        },
        "/cupAuthApi": {
          // target: "http://10.10.87.168:9080/cup-auth",
          // target: "http://192.168.11.91:30755/cup-auth",
          target: "http://192.168.11.91:31310/cup-auth",
          ws: true,
          rewrite: (path) => path.replace(/^\/cupAuthApi/, "/"),
        },
        "/nebulaApi": {
          // target: "http://10.10.87.168:9080/cup-auth",
          // target: "http://192.168.11.91:30755/cup-auth",
          target: "http://192.168.11.91:31310/nebula",
          ws: true,
          rewrite: (path) => path.replace(/^\/nebulaApi/, "/"),
        },
        "/kgManagerApi": {
          // target: "http://172.16.100.54:8200", // 李阳
          target: "http://192.168.11.91:32360",
          ws: true,
          rewrite: (path) => path.replace(/^\/kgManagerApi/, "/"),
        },
        "/kg-service": {
          target: "http://192.168.11.91:31310/kg-service",
          ws: true,
          rewrite: (path) => path.replace(/^\/kg-service/, "/"),
        },
        "/kg-web/sso": {
          target: "http://10.244.1.225:8200/sso",
          ws: true,
          rewrite: (path) => path.replace(/^\/kg-web\/sso/, "/"),
        },
      },
    },
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
        "~": fileURLToPath(new URL("./types", import.meta.url)),
        "@build": fileURLToPath(new URL("/build", import.meta.url)),
      },
    },
    build: {
      sourcemap: true,
      // 消除打包大小超过500kb警告
      chunkSizeWarningLimit: 5000,
      rollupOptions: {
        // 静态资源分类打包
        output: {
          chunkFileNames: "static/js/[name]-[hash].js",
          entryFileNames: "static/js/[name]-[hash].js",
          assetFileNames: "static/[ext]/[name]-[hash].[ext]",
          /** https://rollupjs.org/guide/en/#outputmanualchunks */
          manualChunks(id, { getModuleInfo }) {
            // if (id.includes("lodash")) {
            //   return "lodash";
            // }
            // 解决chunk碎片问题，如开启http2可不用合并chunk碎片
            /** https://www.jianshu.com/p/a0845aa1ff07 */
            // if (
            //   getModuleInfo(id)!.importers.length +
            //     getModuleInfo(id)!.dynamicImporters.length >
            //     1 &&
            //   id.includes("src")
            // ) {
            //   return "mainfest";
            // }
          },
        },
      },
    },
    optimizeDeps: {
      include,
      exclude,
    },
  };
});
